package app.android.com.wuandroidapp.library.util;

import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author wjw
 * Created by admin on 2017/4/22.
 */

public class OkHttp3Utils {
    private static final byte[] LOCKER = new byte[0];
    private static OkHttp3Utils mInstance;
    private OkHttpClient mOkHttpClient;

    private OkHttp3Utils() {
        okhttp3.OkHttpClient.Builder ClientBuilder=new okhttp3.OkHttpClient.Builder();
        ClientBuilder.readTimeout(30, TimeUnit.SECONDS);//读取超时
        ClientBuilder.connectTimeout(10, TimeUnit.SECONDS);//连接超时
        ClientBuilder.writeTimeout(60, TimeUnit.SECONDS);//写入超时
        mOkHttpClient=ClientBuilder.build();
    }


    public static OkHttp3Utils getInstance() {
        if (mInstance == null) {
            synchronized (LOCKER) {
                if (mInstance == null) {
                    mInstance = new OkHttp3Utils();
                }
            }
        }
        return mInstance;
    }

    public Call postAsynHttp(RequestBody formBody,String url) {
        mOkHttpClient=new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
     return mOkHttpClient.newCall(request);

    }

    public Call getAsynHttp(String url) {
        mOkHttpClient=new OkHttpClient();
        Request.Builder requestBuilder = new Request.Builder().url(url);
        //可以省略，默认是GET请求
        requestBuilder.method("GET",null);
        Request request = requestBuilder.build();
        return mOkHttpClient.newCall(request);

    }
}
